/**
 *
 */
package org.insia.smartposse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.form.SearchJobsForm;
import org.insia.smartposse.manager.ProjectManager;
import org.insia.smartposse.manager.list.JobsListManager;
import org.insia.smartposse.manager.list.SkillMapManager;

/**
 * Class for manage the view of All Jobs
 *
 * @author cmougel
 *
 */
public class JobsAction extends DispatchAction {



	@Override
	protected ActionForward dispatchMethod(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp, String name) throws Exception {

		Boolean testMessage = (Boolean) req.getSession().getAttribute("message");

		if (null != testMessage && testMessage){
			ActionMessages messages = new ActionMessages();
			ActionMessage message = new ActionMessage((String)req.getSession().getAttribute("messageMsg"));
			messages.add("messages", message);
			saveMessages(req, messages);
			// Hide the message for After
			req.getSession().setAttribute("message", false);
		}

		if (null == name){
			return listOpen(map, form, req, resp);
		} else {
			return super.dispatchMethod(map, form, req, resp, name);
		}
	}

	/**
	 * View all Job Open
	 *
	 * @param map
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public ActionForward listOpen(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("jobs", new JobsListManager(JobsListManager.NOTCOMPLETE));
		return map.findForward("list");
	}

	/**
	 * View All Job Closed
	 *
	 * @param map
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public ActionForward listClose(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("all", true);
		req.setAttribute("jobs", new JobsListManager(JobsListManager.COMPLETE));
		return map.findForward("list");
	}

	/**
	 * View list of All Jobs, you can add 2 parameters :
	 * <ul>
	 * <li>complete: for define if you want only complete, not complete or all</li>
	 * <li>idproject : for define the project of Job</li>
	 * </ul>
	 * @param map
	 * @param form
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public ActionForward list (ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("all", true);

		JobsListManager jobList = new JobsListManager();
		if (null != req.getParameter("complete")){
			if (JobsListManager.NOTCOMPLETE == new Integer(req.getParameter("complete"))){
				jobList.setComplete(JobsListManager.NOTCOMPLETE);
			}

			if (JobsListManager.COMPLETE == new Integer(req.getParameter("complete"))){
				jobList.setComplete(JobsListManager.COMPLETE);
			}
		}

		if ("" != req.getParameter("idproject") && null != req.getParameter("idproject")){
			jobList.setProject(new ProjectManager(new Integer(req.getParameter("idproject"))));
		}
		req.setAttribute("jobs",jobList);
		return map.findForward("list");
	}

	public ActionForward search(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		SearchJobsForm searchForm = (SearchJobsForm) form;
		req.setAttribute("jobs", new JobsListManager(searchForm));
		req.setAttribute("search", true);
		req.setAttribute("skillMap", new SkillMapManager().getMap());
		return map.findForward("list");
	}


}
